package router

import (
	"github.com/gin-gonic/gin"
	"onerk4/zk2/api/client"
	"onerk4/zk2/api/enit/request"
	"onerk4/zk2/api/enit/response"
	"onerk4/zk2/common/proto/user"
)

func Login(c *gin.Context) {
	var req request.Login

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.Login(c, &user.LoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "登入失败", nil)
		return
	}

	response.SuccessResponse(c, "登入成功", login)
}

func CourseList(c *gin.Context) {

	login, err := client.CourseList(c, &user.CourseListRequest{})
	if err != nil {
		response.ErrorResponse(c, "失败", nil)
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func CourseInfo(c *gin.Context) {
	var req request.CourseInfo

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.CourseInfo(c, &user.CourseInfoRequest{
		CourseId: int64(req.CourseId),
	})
	if err != nil {
		response.ErrorResponse(c, "失败", nil)
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func ReservationCourse(c *gin.Context) {
	var req request.ReservationCourse

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.ReservationCourse(c, &user.ReservationCourseRequest{
		UserId:    int64(userId),
		CourseId:  req.CourseId,
		Data:      req.Data,
		PeopleNum: req.PeopleNum,
		Coach:     req.Coach,
		Memo:      req.Memo,
	})
	if err != nil {
		response.ErrorResponse(c, "失败", nil)
		return
	}
	response.SuccessResponse(c, "ok", login)
}

func QueryReservationCourse(c *gin.Context) {
	var req request.QueryReservationCourse

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.QueryReservationCourse(c, &user.QueryReservationCourseRequest{
		UserId: int64(userId),
		Status: req.Status,
	})
	if err != nil {
		response.ErrorResponse(c, "失败", nil)
		return
	}
	response.SuccessResponse(c, "ok", login)
}

func QueryReservationCourseInfo(c *gin.Context) {
	var req request.QueryReservationCourseInfo

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.QueryReservationCourseInfo(c, &user.QueryReservationCourseRequestInfoRequest{
		UserId:        int64(userId),
		ReservationId: req.ReservationId,
	})
	if err != nil {
		response.ErrorResponse(c, "失败", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", login)
}

func DelQueryReservation(c *gin.Context) {
	var req request.QueryReservationCourseInfo

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.DelQueryReservation(c, &user.DelQueryReservationRequest{
		UserId:        int64(userId),
		ReservationId: req.ReservationId,
	})
	if err != nil {
		response.ErrorResponse(c, "失败", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", login)
}
